﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SaleManagement.Model;
using SaleManagement.Repository;
using SaleManagement.Entity;

namespace SaleManagement.Services
{
  public  class CategoryService
    {
        public ViewModelCategory GetById(int id)
        {
            using (var repo = new CategoryRepository())
            {
                return repo.All.Project().To<ViewModelCategory>().Where(p => p.Id == id).FirstOrDefault();
            }
        }
        public void Delete(int id)
        {
            using (var repo = new CategoryRepository())
            {
                repo.Delete(id); ;
                repo.Save();
            }
        }

        public void InsertOrUpdate(ViewModelCategory model)
        {
            var entity = CategoryMapping.ConvertToEntity(model);
            InsertOrUpdate(entity);
        }
        public void InsertOrUpdate(Category entity)
        {
            using (var repo = new CategoryRepository())
            {
                repo.InsertOrUpdate(entity);
                repo.Save();
            }
        }
        public List<ViewModelCategory> GetAll()
        {
            using (var repo = new CategoryRepository())
            {
                return repo.All.Project().To<ViewModelCategory>().OrderBy(h => h.Name).ToList();
            }
        }
        
    }
}
